home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 1.iso / games / yowsrc12.zip / YOWFNCS.C < prev    next >
C/C++ Source or Header  |  1992-01-24  |  8KB  |  300 lines

  1. //
  2. // YOWFNCS - Yow! PIZZA? no, support functions!
  3. //
  4. // Version: 1.0 Copyright (C) 1991, Lantern Corp.
  5. // Author: Edward Hutchins
  6. // Revisions:
  7. // 09/11/91 removed dependence on file dll - Ed.
  8. //
  9.  
  10. #include "yow.h"
  11.  
  12. //
  13. // globals
  14. //
  15.  
  16. IMPORT HANDLE   hAppInst FROM( yow.c );
  17. IMPORT HWND     hAppWnd FROM( yow.c );
  18. IMPORT CHAR     szAppName[8] FROM( yow.c );
  19. IMPORT CHAR     szDBase[FILENAME_SIZE] FROM( yow.c );
  20. IMPORT CHAR     szEndChar[2] FROM( yow.c );
  21.  
  22. //
  23. // IsLastLine - return true if this line is marked as the "last" one
  24. //
  25.  
  26. BOOL NEAR PASCAL IsLastLine( NPSTR npszLine )
  27. {
  28.     if (!szEndChar[0]) return( TRUE );
  29.     while (*npszLine)
  30.     {
  31.         if (*npszLine == szEndChar[0])
  32.         {
  33.             *npszLine = '\0';
  34.             return( TRUE );
  35.         }
  36.         ++npszLine;
  37.     }
  38.     return( FALSE );
  39. }
  40.  
  41. //
  42. // ReadYowLine - get another buffer full of text
  43. //
  44.  
  45. INT NEAR PASCAL ReadYowLine( INT hFile, NPSTR npszBuff, INT nBuffSize,
  46.                              NPSTR npszLine, INT nLineSize )
  47. {
  48.     NPSTR           npBuff = npszBuff;
  49.     NPSTR           npStart = npszBuff;
  50.     NPSTR           npEnd = npszBuff + nBuffSize - 1;
  51.     INT             nRead = 0;
  52.  
  53.     while (npBuff <= npEnd) switch (*npBuff)
  54.     {
  55.     case '\0':
  56.         {
  57.             INT nCnt = npEnd - npBuff;
  58.             nCnt = (INT)_lread( hFile, npBuff, nCnt );
  59.             if (nCnt > 0) npBuff[nCnt] = '\0';
  60.         }
  61.         if (*npBuff == '\0') goto Done;
  62.         break;
  63.  
  64.     case '\x0d':
  65.     case '\x0a':
  66.         ++nRead;
  67.         *npBuff++ = *npszLine++ = '\0';
  68.         if (*npBuff == '\x0a' || *npBuff == '\x0d') *npBuff++ = '\0';
  69.         goto Done;
  70.  
  71.     default:
  72.         if (--nLineSize <= 0) goto Done;
  73.         *npszLine++ = *npBuff++;
  74.         break;
  75.     }
  76.  
  77. Done:
  78.     while (npBuff <= npEnd) *npStart++ = *npBuff++;
  79.     *npStart = *npszLine = '\0';
  80.     return( nRead );
  81. }
  82.  
  83. //
  84. // GetYowLine - get a line from the lines database
  85. //
  86.  
  87. BOOL NEAR PASCAL GetYowLine( NPSTR npBuff, INT nSize )
  88. {
  89.     INT             hFile = _lopen( szDBase, OF_READ );
  90.     LONG            lSize;
  91.     LOCAL LONG      lPos;
  92.     BOOL            bSuccess = FALSE;
  93.     INT             nCnt, nLine;
  94.     CHAR            szLineBuff[256];
  95.     CHAR            szLine[256];
  96.  
  97.     *npBuff = '\0';
  98.     if (hFile == -1) return( FALSE );
  99.     lSize = _llseek( hFile, 0, 2 );
  100.     if (lSize < 2) goto Abort;
  101.  
  102.     for (nCnt = 10; nCnt; --nCnt)
  103.     {
  104.         lPos = (lPos << 1) ^ GetCurrentTime() ^ MAKELONG( rand(), rand() );
  105.         lPos %= lSize;
  106.         if (_llseek( hFile, lPos, 0 ) != lPos) break;
  107.         szLineBuff[0] = '\0';
  108.         while (ReadYowLine( hFile, szLineBuff, sizeof(szLineBuff),
  109.                             szLine, sizeof(szLine) ) > 0)
  110.         {
  111.             if (IsLastLine( szLine )) break;
  112.         }
  113.         nLine = 0;
  114.         while (ReadYowLine( hFile, szLineBuff, sizeof(szLineBuff),
  115.                             szLine, sizeof(szLine) ) > 0)
  116.         {
  117.             NPSTR npCpy = szLine;
  118.             bSuccess = IsLastLine( szLine );
  119.             if (nLine++)
  120.             {
  121.                 *npBuff++ = '\x0d';
  122.                 *npBuff++ = '\x0a';
  123.                 nSize -= 2;
  124.             }
  125.             while (isspace(*npCpy)) ++npCpy;
  126.             while (*npCpy && --nSize > 0) *npBuff++ = *npCpy++;
  127.             *npBuff = '\0';
  128.             if (bSuccess) goto Abort;
  129.         }
  130.     }
  131.  
  132. Abort:
  133.     if (hFile != -1) _lclose( hFile );
  134.     return( bSuccess );
  135. }
  136.  
  137. //
  138. // InRect - move a rectangle in by one
  139. //
  140.  
  141. VOID NEAR PASCAL InRect( NPRECT npRect )
  142. {
  143.     RECT rect = *npRect;
  144.     ++rect.top; ++rect.left; --rect.bottom; --rect.right;
  145.     *npRect = rect;
  146. }
  147.  
  148. //
  149. // TransRect - move a rectangle along the diagonal
  150. //
  151.  
  152. VOID NEAR PASCAL TransRect( NPRECT npRect, INT nDist )
  153. {
  154.     RECT rect = *npRect;
  155.     rect.top += nDist;
  156.     rect.left += nDist;
  157.     rect.bottom += nDist;
  158.     rect.right += nDist;
  159.     *npRect = rect;
  160. }
  161.  
  162. //
  163. // ShadowRect - draw a shadow rect
  164. //
  165.  
  166. VOID NEAR PASCAL ShadowRect( HDC hDC, NPRECT npRect, HPEN hTL, HPEN hBR )
  167. {
  168.     RECT rect = *npRect;
  169.     --rect.bottom; --rect.right;
  170.     SelectObject( hDC, hTL );
  171.     MoveTo( hDC, rect.right, rect.top );
  172.     LineTo( hDC, rect.left, rect.top );
  173.     LineTo( hDC, rect.left, rect.bottom );
  174.     SelectObject( hDC, hBR );
  175.     LineTo( hDC, rect.right, rect.bottom );
  176.     LineTo( hDC, rect.right, rect.top );
  177.     ++rect.top; ++rect.left;
  178.     *npRect = rect;
  179. }
  180.  
  181. //
  182. // DrawYowBorder - draw a shaded rectangle around the yow display
  183. //
  184.  
  185. VOID NEAR PASCAL DrawYowBorder( HDC hDC, NPRECT npRect )
  186. {
  187.     HPEN            hHiPen = GetStockObject( WHITE_PEN );
  188.     HPEN            hBlackPen = GetStockObject( BLACK_PEN );
  189.     HPEN            hLoPen = CreatePen( 0, 1, RGB( 128, 128, 128 ) );
  190.     RECT            rect = *npRect;
  191.  
  192.     SelectObject( hDC, GetStockObject( NULL_BRUSH ) );
  193.     ShadowRect( hDC, &rect, hHiPen, hLoPen );
  194.     ShadowRect( hDC, &rect, hHiPen, hLoPen );
  195.     InRect( &rect ); InRect( &rect );
  196.     ShadowRect( hDC, &rect, hLoPen, hHiPen );
  197.     ShadowRect( hDC, &rect, hBlackPen, hBlackPen );
  198.     ShadowRect( hDC, &rect, hHiPen, hLoPen );
  199.     if (hLoPen) DeleteObject( hLoPen );
  200. }
  201.  
  202. //
  203. // SwapBitmaps - switch bitmaps around
  204. //
  205.  
  206. VOID NEAR PASCAL SwapBitmaps( HDC hDC, HDC hdcMem, INT x, INT y, INT cx, INT cy )
  207. {
  208.     BitBlt( hdcMem, 0, 0, cx, cy, hDC, x, y, SRCINVERT );
  209.     BitBlt( hDC, x, y, cx, cy, hdcMem, 0, 0, SRCINVERT );
  210.     BitBlt( hdcMem, 0, 0, cx, cy, hDC, x, y, SRCINVERT );
  211. }
  212.  
  213. //
  214. // Yow - yow!
  215. //
  216.  
  217. VOID FAR PASCAL Yow( INT nVirtKey )
  218. {
  219.     HDC             hDC = CreateScreenDC();
  220.     HDC             hdcMem = CreateCompatibleDC( hDC );
  221.     HBITMAP         hbm = NULL, hbmOld = NULL;
  222.     HFONT           hfont = NULL, hfontOld = NULL;
  223.     INT             x, y;
  224.     INT             cx = GetSystemMetrics( SM_CXSCREEN );
  225.     INT             cy = GetSystemMetrics( SM_CYSCREEN );
  226.     RECT            rect;
  227.     BOOL            bSuccess = FALSE;
  228.     CHAR            szBuff[2048];
  229.  
  230.     // fetch the data
  231.     if (!GetYowLine( szBuff, sizeof(szBuff) ))
  232.     {
  233.         MessageBeep( 0 );
  234.         return;
  235.     }
  236.  
  237.     hfont = CreateFont( cx / 40, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE,
  238.                         ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_STROKE_PRECIS,
  239.                         PROOF_QUALITY, DEFAULT_PITCH | FF_ROMAN, "Tms Rmn" );
  240.     if (!hfont) goto Abort;
  241.  
  242.     // calculate the bounding box
  243.     rect.left = 0; rect.right = cx - 56;
  244.     rect.top = 0; rect.bottom = cy - 20;
  245.  
  246.     SetBkMode( hDC, TRANSPARENT );
  247.     hfontOld = SelectObject( hDC, hfont );
  248.     DrawText( hDC, szBuff, -1, &rect, YOW_STYLE | DT_CALCRECT );
  249.     SelectObject( hDC, hfontOld );
  250.     hfontOld = HNULL;
  251.  
  252.     // calculate the width and height, and the centered offset on the screen
  253.     x = (cx - (56 + rect.right - rect.left)) / 2;
  254.     cx = 40 + rect.right - rect.left; // kludge in some padding
  255.     y = (cy - (20 + rect.bottom - rect.top)) / 2;
  256.     cy = 4 + rect.bottom - rect.top;
  257.  
  258.     // create the offscreen bitmap
  259.     hbm = CreateCompatibleBitmap( hDC, cx + 16, cy + 16 );
  260.     if (!hbm) goto Abort;
  261.     hbmOld = SelectObject( hdcMem, hbm );
  262.     hfontOld = SelectObject( hdcMem, hfont );
  263.  
  264.     // set up the bitmap background
  265.     rect.top = rect.left = 0;
  266.     rect.right = 16 + cx; rect.bottom = 16 + cy;
  267.     FillRect( hdcMem, &rect, GetStockObject( LTGRAY_BRUSH ) );
  268.     DrawYowBorder( hdcMem, &rect );
  269.  
  270.     // draw the fancy text
  271.     SetBkMode( hdcMem, TRANSPARENT );
  272.     rect.top = rect.left = 8;
  273.     rect.right = 8 + cx; rect.bottom = 8 + cy;
  274.     TransRect( &rect, 2 );
  275.     SetTextColor( hdcMem, RGB(255,255,255) );
  276.     DrawText( hdcMem, szBuff, -1, &rect, YOW_STYLE );
  277.     TransRect( &rect, -2 );
  278.     SetTextColor( hdcMem, RGB(128,128,128) );
  279.     DrawText( hdcMem, szBuff, -1, &rect, YOW_STYLE );
  280.     TransRect( &rect, 1 );
  281.     SetTextColor( hdcMem, RGB(0,0,0) );
  282.     DrawText( hdcMem, szBuff, -1, &rect, YOW_STYLE );
  283.  
  284.     // keep the bitmap up till the key is released
  285.     SwapBitmaps( hDC, hdcMem, x, y, 16 + cx, 16 + cy );
  286.     while (GetAsyncKeyState( nVirtKey ) < 0) ;
  287.     SwapBitmaps( hDC, hdcMem, x, y, 16 + cx, 16 + cy );
  288.  
  289.     bSuccess = TRUE;
  290.  
  291. Abort:
  292.     if (hbmOld) SelectObject( hdcMem, hbmOld );
  293.     if (hfontOld) SelectObject( hdcMem, hfontOld );
  294.     if (hdcMem) DeleteDC( hdcMem );
  295.     if (hDC) DeleteDC( hDC );
  296.     if (hfont) DeleteObject( hfont );
  297.     if (hbm) DeleteObject( hbm );
  298.     if (!bSuccess) MessageBeep( 0 );
  299. }
  300.